package com.nicekun.calc;

import com.nicekun.calc.core.CalcLexer;
import com.nicekun.calc.core.CalcParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static void main(String[] args) throws IOException {
        String inputFile = null;
        if (args.length > 0) {
            inputFile = args[0];
        }
        InputStream is = System.in;

        if (inputFile != null) is = new FileInputStream(inputFile);
        CalcLexer calcLexer = new CalcLexer(CharStreams.fromStream(is));
        CalcParser calcParser = new CalcParser(new CommonTokenStream(calcLexer));
        ParseTree t = calcParser.prog();

        System.out.println(t.toStringTree(calcParser));

        EvalVisitor evalVisitor = new EvalVisitor();
        evalVisitor.visit(t);
    }
}
